<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<html>
<script type="text/javascript">


function showApp(str)
{ 
	
var xmlHttp;

xmlHttp=new XMLHttpRequest();
if (xmlHttp==null)
  {
  alert ("Your browser does not support AJAX!");
  return;
  }
var url="application_xml.jsp";
url=url+"?id="+str;
xmlHttp.onreadystatechange=function()
{ 
if (xmlHttp.readyState==4)
{
var xmlDoc=xmlHttp.responseXML.documentElement;
document.getElementById("userid").innerHTML= xmlDoc.getElementsByTagName("userid")[0].childNodes[0].nodeValue;
document.getElementById("name").innerHTML= xmlDoc.getElementsByTagName("name")[0].childNodes[0].nodeValue;
document.getElementById("res").innerHTML= xmlDoc.getElementsByTagName("res")[0].childNodes[0].nodeValue;
document.getElementById("cit").innerHTML= xmlDoc.getElementsByTagName("cit")[0].childNodes[0].nodeValue;
document.getElementById("spec").innerHTML= xmlDoc.getElementsByTagName("spec")[0].childNodes[0].nodeValue;
alert("123")
document.getElementById("address").innerHTML= xmlDoc.getElementsByTagName("address")[0].childNodes[0].nodeValue;
document.getElementById("city").innerHTML= xmlDoc.getElementsByTagName("city")[0].childNodes[0].nodeValue;
document.getElementById("street").innerHTML= xmlDoc.getElementsByTagName("street")[0].childNodes[0].nodeValue;
document.getElementById("areacode").innerHTML= xmlDoc.getElementsByTagName("areacode")[0].childNodes[0].nodeValue;
document.getElementById("zipcode").innerHTML= xmlDoc.getElementsByTagName("zipcode")[0].childNodes[0].nodeValue;
document.getElementById("state").innerHTML= xmlDoc.getElementsByTagName("state")[0].childNodes[0].nodeValue;
document.getElementById("phoneno").innerHTML= xmlDoc.getElementsByTagName("phoneno")[0].childNodes[0].nodeValue;
document.getElementById("telecode").innerHTML= xmlDoc.getElementsByTagName("telecode")[0].childNodes[0].nodeValue;

document.getElementById("discip").innerHTML= xmlDoc.getElementsByTagName("discip")[0].childNodes[0].nodeValue;
document.getElementById("uni").innerHTML= xmlDoc.getElementsByTagName("uni")[0].childNodes[0].nodeValue;
document.getElementById("loc").innerHTML= xmlDoc.getElementsByTagName("loc")[0].childNodes[0].nodeValue;
document.getElementById("date").innerHTML= xmlDoc.getElementsByTagName("date2")[0].childNodes[0].nodeValue;

}
}
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}

function GetXmlHttpObject()
{
var xmlHttp=null;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
return xmlHttp;
}

</script>



<title>Application Page</title>
</head>
<body>

<%
String spec = request.getParameter("specialization");
String dis = request.getParameter("discipline");

%>
	<%-- Import the java.sql package --%>
	<%@ page import="java.sql.*"%>
	<%-- -------- Open Connection Code -------- --%>
	<%
            
   Connection conn = null;
   PreparedStatement pstmt = null;
   ResultSet rs = null;
   try {
    // Registering Postgresql JDBC driver with the DriverManager
    Class.forName("org.postgresql.Driver");
   // Open a connection to the database using DriverManager
   conn = DriverManager.getConnection(
     "jdbc:postgresql://localhost/admissions?" +
        "user=postgres&password=27428572");
 %>
	<%
 // Begin transaction
      conn.setAutoCommit(false);
 %>
	<table>
		<tr>
			<th>ID</th>
			<th>Name</th>
			<th>Average</th>
			<th></th>
			<th></th>
			<th></th>
		<tr>
			<%

Statement stmt = conn.createStatement();
if(request.getParameter("ungraded") != null)
{
 	
	rs = stmt.executeQuery("SELECT student_id,student_name FROM list_application WHERE reviewer_id='"+request.getParameter("id")
			+"' AND graded='false'");
	if(rs.next())
	{
		do
		{
			
			String studentId = Integer.toString(rs.getInt(1));
			String studentName = rs.getString(2);
			%><td><%=studentId%></td>
			<td><%=studentName%></td>
			<%
		Statement stmt2 = conn.createStatement();
		ResultSet rs2 = stmt2.executeQuery("SELECT user_id FROM user_link WHERE student_id='"+rs.getInt(1)+"'");
		rs2.next();
		int userId = rs2.getInt(1);
		rs2 = stmt2.executeQuery("SELECT average, student_status FROM student_status WHERE user_id='"+userId+"'");
		rs2.next();
		%>
			<td>None</td>
			<% 
		String status = rs2.getString(2);
		if(status.equals("Pending"))
		{
		%>
			<td><html:link action="protected/admins/finalDecision">
					<html:param name="id" value="<%=Integer.toString(userId)%>" />
					<html:param name="dec" value="Accept" />Accept</html:link></td>

			<td><html:link action="protected/admins/finalDecision">
					<html:param name="id" value="<%=Integer.toString(userId)%>" />
					<html:param name="dec" value="Reject" />Reject</html:link></td>
			<%
		}
		else if(status.equals("Accept"))
		{
		%>
			<td><html:link action="protected/admins/finalDecision">
					<html:param name="id" value="<%=Integer.toString(userId)%>" />
					<html:param name="dec" value="Reject" />Reject</html:link></td>
			<td></td>
			<%
		}
		else
		{
		%>
			<td></td>
			<td><html:link action="protected/admins/finalDecision">
					<html:param name="id" value="<%=Integer.toString(userId)%>" />
					<html:param name="dec" value="Accept" />Accept</html:link></td>
			<%
		}
		%>
		<td><input type="button" value="Show Application" onclick="showApp(<%=Integer.toString(userId)%>)" /></td>
		</tr>
	<%	
	}
	while (rs.next());
	}
	else
	{
	out.println("All Application has been graded.");
	}
}
else if(request.getParameter("graded")  != null)
{
	int count = 0;
	rs = stmt.executeQuery("SELECT student_id,student_name FROM list_application WHERE reviewer_id='"+request.getParameter("id")
			+"' AND graded='true'");
	if(rs.next())
	{
	%><tr>
			<%
	do
	{
		
		String studentId = Integer.toString(rs.getInt(1));
		String studentName = rs.getString(2);
		%><td><%=studentId%></td>
		<td><%=studentName%></td>
		<%
		Statement stmt2 = conn.createStatement();
		ResultSet rs2 = stmt2.executeQuery("SELECT user_id FROM user_link WHERE student_id='"+rs.getInt(1)+"'");
		rs2.next();
		int userId = rs2.getInt(1);
		rs2 = stmt2.executeQuery("SELECT average, student_status FROM student_status WHERE user_id='"+userId+"'");
		rs2.next();
		%>
			<td><%=rs2.getString(1)%></td>
			<% 
		String status = rs2.getString(2);
		if(status.equals("Pending"))
		{
		%>
			<td><html:link action="protected/admins/finalDecision">
					<html:param name="id" value="<%=Integer.toString(userId)%>" />
					<html:param name="dec" value="Accept" />Accept</html:link></td>

			<td><html:link action="protected/admins/finalDecision">
					<html:param name="id" value="<%=Integer.toString(userId)%>" />
					<html:param name="dec" value="Reject" />Reject</html:link></td>

			<%
		}
		else if(status.equals("Accept"))
		{
		%>
			<td><html:link action="protected/admins/finalDecision">
					<html:param name="id" value="<%=Integer.toString(userId)%>" />
					<html:param name="dec" value="Reject" />Reject</html:link></td>
			<td></td>
			<%
		}
		else
		{
		%>
			<td></td>
			<td><html:link action="protected/admins/finalDecision">
					<html:param name="id" value="<%=Integer.toString(userId)%>" />
					<html:param name="dec" value="Accept" />Accept</html:link></td>
			<%
		}
		%>
		<td><input type="button" value="Show Application" onclick="showApp(<%=Integer.toString(userId)%>)" /></td>
		</tr>
	<%	
	}
	while (rs.next());
	%>
	<%
	}
	else
	{
	out.println("No Application has been graded.");
	}
		
}
else if(!(spec == null))
{
	//print out specialization
	out.println("Print out all the appliants in "+spec+" : <br> ID      Name<br>");
	ResultSet specId = stmt.executeQuery("SELECT id FROM specializations WHERE name = '" + spec + "'");
	//out.println(specId.getInt(1));
	if(specId.next())
	{
	ResultSet studentId = stmt.executeQuery("SELECT id,name FROM student WHERE spec = '"+specId.getInt(1)+"'");
  	while(studentId.next())
	{
		out.println(studentId.getInt(1)+"     "+studentId.getString(2));
		out.println("<br>");
	}
	}
		
	
}
else if(!(dis == null))
{
	//print out discipline
	out.println("Print out all the appliants in "+dis+" : <br> ID      Name<br>");
	ResultSet disId = stmt.executeQuery("SELECT id FROM disciplines WHERE name = '" + dis + "'");
//	ResultSet studentId = null;
//	ResultSet studentInfo = null;
	Statement stmt2 = conn.createStatement();
	Statement stmt3 = conn.createStatement();

	if(disId.next())
	{
		ResultSet degrees = stmt.executeQuery("SELECT student FROM degrees WHERE name = '"+disId.getInt(1)+"'");
	while(degrees.next())
	{
		ResultSet studentInfo = stmt2.executeQuery("SELECT name FROM student WHERE id = '"+degrees.getInt(1)+"'");
		if(studentInfo.next())
		{
			out.println(degrees.getInt(1) +"   "+studentInfo.getString(1));
			out.println("<br>");
		}
	}
	}
//	studentId.close();
}
else
{
	//print out application
	out.println("Print out all the appliants : <br>");
	ResultSet studentId = stmt.executeQuery("SELECT id,name FROM student");
	while (studentId.next())
	{
		out.println("ID : "+studentId.getInt(1)+ "    Name : "+studentId.getString(2));
		out.println("<br>");
	}
}
}
	catch (SQLException e){
				throw new RuntimeException(e);
			}

finally{
		if(rs != null){
					try{ 
						rs.close();
	 
					}catch (SQLException e){ }
					rs = null;
					}
				if(pstmt != null){
					try{
						pstmt.close();
					}catch(SQLException e){}
					pstmt = null;
				}
				if(conn != null){
					try{
						conn.close();
					}catch (SQLException e) { }
					conn = null;
				}

		}

%>
</table>
<b><span id="userid"></span><span id="name"> </span></b><br >
<span id="res"></span><br>
<span id="cit"></span><br>
<span id="spec"></span><br>

<span id="address"></span><br>
<span id="street"></span>
<span id="city"></span>
<span id="state"></span>
<span id="telecode"></span><br>
<span id="areacode"></span>
<span id="zipcode"></span><br>
<span id="phoneno"></span><br>

<span id="discip"></span><br>
<span id="uni"></span><br>
<span id="loc"></span><br>
<span id="date"></span><br>

</body>
</html>
